/**
 * @Author:  renyc
 * @Date: 2022年12月1日14:50:59
 */
'use strict';

import Util from "../utils/Util";
import {OverlayEventType} from "../event/EventType";
import OverlayType from "./OverlayType";
import EventFactory from "../event/EventFactory";

class Overlay{

    constructor(){
        this._id = Util.uuid();
        this._delegate = undefined;
        this._type = OverlayType.NONE;

        this._layer = undefined;        

        this._event = EventFactory.createOverlayEvent();
    }

    /** 获取Overlay的id */
    get id(){
        return this._id;        
    }
    
    /**
     * 获取Overlay的类型
     */
    get type(){
        return this._type;
    }

    get delegate(){
        return this._delegate;
    }

    /**
     * 注册Overlay事件监听
     * @param {*} type 
     * @param {*} listener 
     */
     on(type, listener){
        return this._event.on(type, listener, this);
    }

    /**
     * 注销Overlay事件监听
     * @param {*} type 
     * @param {*} listener 
     */
    off(type, listener){
        return this._event.off(type, listener, this);
    }

    /**
     * 将Layer挂载到Viewer上
     * @Override
     * @param {Layer} layer 
     */
     attach(layer){
        this._layer = layer;      //记录当前Layer所属的viewer
        if(!this._attach()){
            return false;
        }
        //触发LayerEventType.ADD事件
        this._fire(OverlayEventType.ADD, {obj:this});
        return true;
    }

    /**
     * 将Layer从Viewer上卸载
     * @Override
     */
    detach(){
        //触发LayerEventType.REMOVE事件
        this._fire(OverlayEventType.REMOVE, {obj:this});

        this._detach();
        this._layer = undefined;        
    }

    /**
     * 将Layer挂载到Viewer上(内部方法)
     * @private
     * @Override
     */
    _attach(){}

    /**
     * 将Layer从Viewer上卸载(内部方法)
     * @private
     * @Override
     */
    _detach(){}


    /**
     * 触发Overlay事件
     * @private
     * @param {*} type 
     * @param {*} params 
     * @returns {Overlay}引用
     */
    _fire(type, params) {
        this._event.fire(type, params)
        return this;
    }

}

export default Overlay
